package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.Statistics_chargeDto;
import edu.suda.xsap.dto.Statistics_totcourseDto;
import edu.suda.xsap.entity.Statistics_totcourse;
import edu.suda.xsap.service.Statistics_chargeService;
import edu.suda.xsap.service.Statistics_totcourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Year;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/statistics_totcourse")
@Slf4j
public class Statistics_totcourseController {

    @Autowired
    private Statistics_totcourseService statistics_totcourseService;

    @GetMapping("/year")
    public Result year(@RequestParam("year1")Year year1, @RequestParam("year2")Year year2) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<Statistics_totcourseDto> statistics_totcourseDtos = statistics_totcourseService.findYEAR(year1, year2);
            //
            return Result.success(statistics_totcourseDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @GetMapping("/month")
    public Result month(@RequestParam("year")Year year) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<Statistics_totcourseDto> statistics_totcourseDtos =
                    statistics_totcourseService.findMONTH(year);
            Map<String, Integer> pair = new HashMap<>();
            Integer length = 12;
            for (int i = 0; i < statistics_totcourseDtos.size(); i++) {
                Statistics_totcourseDto jsonObject = statistics_totcourseDtos.get(i);
                pair.put(String.valueOf(jsonObject.getMonth().getValue()), jsonObject.getTotcourse());
            }
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                if (pair.get(String.valueOf(i + 1)) == null) {
                    list.add( 0);
                } else {
                    list.add(pair.get(String.valueOf(i + 1)));
                }
            }
            return Result.success(list);

        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @GetMapping("/quarter")
    public Result quarter(@RequestParam("year")Year year) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<Statistics_totcourseDto> statistics_totcourseDtos = statistics_totcourseService.findQUARTER(year);
            //
            return Result.success(statistics_totcourseDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

}
